package jp.co.menox.controller.tmapp.borrow;

import jp.co.menox.tmapp.service.LendingHistoryService;
import jp.co.menox.tmapp.util.AjaxResponse;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.gson.Gson;

public class CheckConclusionController extends Controller {

    @Override
    public Navigation run() throws Exception {
        //--------------------------------------------------------------------
        //  1. リクエストからパラメータ取得
        //--------------------------------------------------------------------
        long id = Long.valueOf(request.getParameter("id"));
        int beforeStatus = Integer.valueOf(request.getParameter("before_status"));
        
        //--------------------------------------------------------------------
        //  2. ステータス更新
        //--------------------------------------------------------------------
        LendingHistoryService service = new LendingHistoryService();
        boolean isConcluded = service.checkConclusion(id, beforeStatus);

        
        //--------------------------------------------------------------------
        //  3. JSON変換
        //--------------------------------------------------------------------
        AjaxResponse ajaxResult = new AjaxResponse();
        ajaxResult.success = isConcluded;
        String json = new Gson().toJson(ajaxResult);
        
        
        //--------------------------------------------------------------------
        //  4. レスポンス設定
        //--------------------------------------------------------------------
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, OPTIONS");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        
        if (isConcluded) {
            response.getWriter().println(json);
        }
        
        
        return null;
    }
}
